<template>
	<view>
		<view class="postcomment">
			<uni-list v-for="item,index in list" :key="index">
				<uni-badge :text="item.count">
					<uni-list-item :title="item.title" :note="item.content"
						:rightText="item.date+item.name"></uni-list-item>
				</uni-badge>
			</uni-list>
			<input disabled class="placeholder" placeholder="评论 : "></input>
			<button class="postbutton" size="mini" @click="showPostDialog">发表</button>
		</view>
		<uni-popup ref="popup" type='dialog'>
			<uni-popup-dialog title="发表评论" confirmText="发表" @confirm="postMessage">
				<view style="width: 100%;">
					<view>标题：</view>
					<uni-easyinput type="textarea" v-model="bt" placeholder="请输入标题" />
					<view>内容：</view>
					<uni-easyinput type="textarea" v-model="message" placeholder="请输入内容" />
				</view>
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bt: '',
				comment: [],
				message: '',
				scrollheight: 100,
				"list": []
			}
		},
		methods: {
			onPullDownRefresh() {
				// 写在这里的代码将会在下拉刷新时执行
				console.log('下拉刷新触发');
				// 比如重新加载数据				
				this.loadList();
			},
			onReachBottom() {
						uni.showLoading()
						setTimeout(()=>{
							this.loadList();
						},1000);
					},
			loadList() {
				// 模拟数据加载
				console.log('输出停止');
				setTimeout(() => {
					uni.stopPullDownRefresh(); // 停止下拉刷新
					// 更新数据
					this.reload();
				}, 3000); // 假设数据加载需要2秒钟
			},
			showPostDialog() {
				this.$refs.popup.open()
			},
			postMessage(e) {
				let list = {
					"title": this.bt,
					"content": this.message,
					"date": new Date().toISOString().substring(0, 10),
					"name": uni.getStorageSync('list').name
				}
				uni.request({
					url: 'http://localhost:8090/freshmenapp/message/addone',
					method: 'POST',
					data: list,
					success: res => {
						// console.log(res);
						if (res.data.success) {
							uni.showToast({
								title: '发表成功',
							});
						} else {
							uni.showToast({
								title: '发表失败',
								icon: "error",
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '发表失败',
							icon: "error"
						});
					},
					complete: () => {
						uni.startPullDownRefresh();
					}
				})
			},
			reload() {
				console.log('加载列表数据');
				uni.showLoading({
					title: '加载中',
					mask: false
				});
				//urequest
				uni.request({
					url: 'http://localhost:8090/freshmenapp/message/list',
					method: 'POST',
					data: {},
					success: res => {
						// console.log(res);
						this.list = res.data.list
						uni.showToast({
							title: '刷新成功',
							icon: 'none'
						});
						setTimeout(() => {
							uni.hideLoading()
						}, 1000)
					},
					fail: () => {
						uni.showToast({
							title: '失败',
							icon: 'none'
						});
					},
					complete: () => {}
				});
			}
		}
	}
</script>

<style>

</style>